/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import DAL.AdminBean;
import DAL.ExitsBean;
import java.io.*;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

/**
 *
 * @author Mr-aUgust
 */
@WebServlet(name = "ControllerAdmin", urlPatterns = {"/ControllerAdmin"})
public class ControllerAdmin extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        AdminBean ab=new AdminBean();
         HttpSession session = request.getSession();
        if(request.getParameter("login")!=null)
        {
            if(request.getParameter("login").equals("Login"))
            {
                String user=request.getParameter("user");
                String pass=request.getParameter("pass");
               // String phone=request.getParameter("phone");
                boolean rs=ab.LoginAdmin(user,pass);
                if(rs==true)
                {
                    session.setAttribute("Admin",user);
                    session.setAttribute("para","Login Success!");
                    response.sendRedirect("Admin/Notice.jsp");
                    //out.println("<script>alert('Login success!');window.location='Admin/management.jsp';</script>");
                }
                else
                {
                    out.println("<script>alert('Username or password not correct!');javascript:window.history.back(-1);</script>");
                }
            }
        }
         if(request.getParameter("createAmdmin")!=null)
        {
            if(request.getParameter("createAmdmin").equals("Create Amdmin"))
            {
                    String user=request.getParameter("username");
                    String pass=request.getParameter("password");
                    String name=request.getParameter("fullname");
                    String email=request.getParameter("email");
                    String address=request.getParameter("address");
                    String phone=request.getParameter("phone");
                    int role=Integer.parseInt(request.getParameter("role"));
                    boolean rs=ab.InsertAdmin(user,pass,name,email,address,phone,role);
                    if(rs==true)
                    {
                        out.println("<script>alert('Add new success!');window.location='Admin/management.jsp?page=ManageAdmin';</script>");
                    }
                    else
                    {
                        out.println("<script>alert('Error data! please input information correct');javascript:window.history.back(-1);</script>");
                    }
            }
         }
        if(request.getParameter("edit")!=null)
        {
            if(request.getParameter("edit").equals("Edit"))
            {
                    String user=request.getParameter("username");
                    String name=request.getParameter("fullname");
                    String email=request.getParameter("email");
                    String address=request.getParameter("address");
                    String phone=request.getParameter("phone");
                    String role=request.getParameter("role");
                    
                    session.setAttribute("username",user);
                    session.setAttribute("fullname",name);
                    session.setAttribute("email",email);
                    session.setAttribute("address",address);
                    session.setAttribute("phone",phone);
                    session.setAttribute("role",role);
                    session.setAttribute("status","True");
                    response.sendRedirect("Admin/management.jsp?page=ManageAdmin");
            }
       }
       if(request.getParameter("update")!=null)
        {
            if(request.getParameter("update").equals("Update"))
            {
                    String user=request.getParameter("username");
                    String name=request.getParameter("fullname");
                    String email=request.getParameter("email");
                    String address=request.getParameter("address");
                    String phone=request.getParameter("phone");
                    int role=Integer.parseInt(request.getParameter("role"));
                    boolean rs=ab.UpdateAdmin(user,name,email,address,phone,role);
                    if(rs==true)
                    {
                        session.removeAttribute("username");
                        session.removeAttribute("fullname");
                        session.removeAttribute("email");
                        session.removeAttribute("address");
                        session.removeAttribute("phone");
                        session.removeAttribute("role");
                        session.removeAttribute("status");
                        out.println("<script>alert('Update success!');window.location='Admin/management.jsp?page=ManageAdmin';</script>");
                    }
                    else
                    {
                        out.println("<script>alert('Error data in update! please input information correct ');javascript:window.history.back(-1);</script>");
                    }
            }
       }
       if(request.getParameter("Cancelupdate")!=null)
        {
           if(request.getParameter("Cancelupdate").equals("Cancel Update"))
            {
                session.removeAttribute("username");
                session.removeAttribute("fullname");
                session.removeAttribute("email");
                session.removeAttribute("address");
                session.removeAttribute("phone");
                session.removeAttribute("role");
                session.removeAttribute("status");
                response.sendRedirect("Admin/management.jsp?page=ManageAdmin");
            }
          }
      if(request.getParameter("delete")!=null)
         {
           if(request.getParameter("delete").equals("Delete"))
            {
               
               String user=request.getParameter("username");
               String admin=session.getAttribute("Admin").toString();             
               if(user.equals(admin))
               {
                   out.println("<script>alert('you can not delete own!');window.location='Admin/management.jsp?page=ManageAdmin';</script>");
               }
               else
               {
                   ExitsBean eb=new ExitsBean();
                   if(eb.ExitsAdmin(user)==true)
                   {
                        out.println("<script>alert('you can not permission delete Admin!');window.location='Admin/management.jsp?page=ManageAdmin';</script>");
                   }
                   else
                   {
                       boolean rs=ab.DeleteAdmin(user);
                       if(rs==true)
                       {
                           out.println("<script>alert('Delete employee success!');window.location='Admin/management.jsp?page=ManageAdmin';</script>");
                       }
                       else
                       {
                           out.println("<script>alert('Error! please choose correct employee other ');javascript:window.history.back(-1);</script>");
                       }
                   }
               }
           }
       }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
